import { action, observable } from 'mobx'

import { getUserInfo, getAuthCode, logout, changeInfo, getSearchList, getHotList, getSearchTips, getArticleDetail,Detail,getRankDropList } from '../../services/index'
//引入
import { ISearchRankItem, ISearchArticleItem } from '../../untils/interface'
class Mine {

    //我的页面的数据
    @observable
    info = {}

    //热搜
    @observable
    hostList: string[] = []

    //历史搜索
    @observable
    historyList: string[] = []

    //搜索结果下的数据
    @observable
    searchRankList: ISearchRankItem[] = []
    @observable
    searchArticleList: ISearchArticleItem[] = []
    @observable
    searchRecommendRankList: ISearchRankItem[] = []
    @observable
    searchRecommendArticleList: ISearchArticleItem[] = []

    //关键词搜索
    @observable
    searchTipList: string[] = []

    //文章详情
    @observable
    detail: { [key: string]: string } = {};

    //排行详情
    @observable
    rankId:string[] = []

    @observable
    arr:string[] = [];


    //渲染我的页面
    @action
    async getInfo() {
        let result = await getUserInfo()
        this.info = result.data
        // console.log(this.info)
    }
    //发送手机验证码
    @action
    async sendCode(phone: string) {
        let result = await getAuthCode(phone);
        if (result) {

        }
    }
    //修改昵称
    @action
    async changeInfo(info: string) {
        let result = await changeInfo(info)
    }
    //退出登录
    @action
    async delD() {
        return await logout()
    }

    //热搜
    @action
    async getHotList() {
        let result = await getHotList()
        if (result) {
            this.hostList = result.data.dataList
        }
    }

    //关键词提示
    @action
    async getSearchTips(keyword: string) {
        let result = await getSearchTips(keyword)
        if (result) {
            this.searchTipList = result.data.dataList
        }
    }

    //历史搜索
    @action
    getHistory() {
        let history: string | string[] | null = localStorage.getItem('search_history')
        if (history) {
            history = JSON.parse(history)
        } else {
            history = []
        }
        this.historyList = history as string[]
    }

    //搜索结果
    @action
    async getSearchResult(keyword: string) {
        let result = await getSearchList(keyword);
        if (result) {
            // debugger
            this.searchRankList = result.data.rankList;
            this.searchArticleList = result.data.articleList;
            this.searchRecommendRankList = result.data.recommendRankList;
            this.searchRecommendArticleList = result.data.recommendArticleList;
        }
    }

    //文章详情
    @action
    async getArticleDetail(articleId: string) {
        let result = await getArticleDetail(articleId)
        if (result) {
            this.detail = result.data;
        }
    }
    //排行详情
    @action
    async Details(rankId:string){
        let result=await Detail(rankId)
        let arr= await getRankDropList(rankId)
        console.log(result,arr)
        if(result){
            this.rankId=result.data.rankDataList
            this.arr=arr.data.rankItemGroup
            
        }
    }
}
export default Mine